package de.hhkk.util.formatter;

import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;

public class NumberVerifyKeyListener implements VerifyListener, VerifyKeyListener {

    public void verifyText(VerifyEvent verifyevent) {
        verify(verifyevent);
    }

    public void verifyKey(VerifyEvent verifyevent) {
        verify(verifyevent);
    }

    private void verify (VerifyEvent event) {
        event.doit = true;

        String text = event.text;
        char[] chars = text.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (!Character.isDigit(chars[i])) {
                event.doit = false;
                break;
            }
        }
    }

}

